package com.hello.hellowebview

import android.os.Bundle
import android.webkit.WebView
//import org.chromium.android_webview.sdk.CustomWebView as WebView;

import android.webkit.WebViewClient
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.compose.ui.tooling.preview.Preview
import com.hello.hellowebview.ui.theme.HelloWebviewTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            HelloWebviewTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    WebViewScreen(Modifier.padding(innerPadding))
                }
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun WebViewScreen(modifier: Modifier = Modifier) {
    var url by remember { mutableStateOf("https://www.baidu.com") } // 用户输入的 URL
    var isLoading by remember { mutableStateOf(false) } // 网页加载状态
    var statusMessage by remember { mutableStateOf("加载状态") } // 状态栏消息
    var progress by remember { mutableStateOf(0) } // 加载进度
    var pageTitle by remember { mutableStateOf("网页标题") } // 网页标题
    val context = LocalContext.current

    // 使用 remember 创建 WebView 实例
    val webView = remember {
        WebView(context).apply {
            settings.javaScriptEnabled = true
            loadUrl(url) // 初始加载默认 URL
        }
    }

    Column(
        modifier = modifier.fillMaxSize(),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        // 状态栏
        Text(
            text = statusMessage,
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp),
            color = androidx.compose.ui.graphics.Color.Gray
        )

        // 网页标题
        Text(
            text = pageTitle,
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp),
            color = androidx.compose.ui.graphics.Color.Black
        )

        // 加载进度条
        if (isLoading) {
            androidx.compose.material3.LinearProgressIndicator(
                progress = progress / 100f,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 8.dp)
            )
        }

        // 地址输入框
        OutlinedTextField(
            value = url,
            onValueChange = { newUrl ->
                url = newUrl // 更新用户输入的 URL
            },
            label = { Text("输入 URL") },
            modifier = Modifier.fillMaxWidth()
        )
        // 按钮行
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            // 加载 URL 按钮
            Button(
                onClick = {
                    webView.loadUrl(url) // 加载用户输入的 URL
                },
                modifier = Modifier.weight(1f)
            ) {
                Text("加载 URL")
            }
            // 刷新按钮
            Button(
                onClick = {
                    webView.reload() // 刷新当前页面
                },
                modifier = Modifier.weight(1f)
            ) {
                Text("刷新")
            }
        }
        // WebView 组件
        Box(modifier = Modifier.weight(1f)) {
            AndroidView(
                factory = { webView },
                modifier = Modifier.fillMaxSize()
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun WebViewScreenPreview() {
    HelloWebviewTheme {
        WebViewScreen()
    }
}